package com.zry.yard.presenter

import android.annotation.SuppressLint
import android.content.Intent
import com.tsy.sdk.myokhttp.response.DownloadResponseHandler
import com.tsy.sdk.myokhttp.response.RawResponseHandler
import com.zry.yard.MainActivity
import com.zry.yard.R
import com.zry.yard.R.id.*
import com.zry.yard.adapter.HomePageAdapter
import com.zry.yard.animation.RecyclerViewUtilKt
import com.zry.yard.been.BasicBean
import com.zry.yard.been.HttpResponse
import com.zry.yard.been.LoginInfoBean
import com.zry.yard.been.OperaID
import com.zry.yard.databinding.FragmentHomeBinding
import com.zry.yard.retrofit.BaseApiRetrofit
import com.zry.yard.retrofit.HttpServer
import com.zry.yard.retrofit.ShowUserView
import com.zry.yard.ui.activity.*
import com.zry.yard.utils.*
import org.json.JSONObject

class HomePresenter(private val mainActivity: MainActivity, private val homeBinding: FragmentHomeBinding) :ShowUserView{


    private var password: String? = null
    private var username: String? = null
    private var loginInfo: LoginInfoBean? = null


    private var recyclerViewUtilKt: RecyclerViewUtilKt? = null
    private var homePageAdapter: HomePageAdapter? = null
    private var basicList: MutableList<BasicBean>? = null


    fun initDataView() {
        password = ACacheUtil().getPassword()
        username = ACacheUtil().getUsername()
        if (password != null && password != "" && username != null && username != "") {
            if (NetWorkUtils().isNetworkAvailable(mainActivity)){
                HttpServer(this).loginApi(username!!, password!!, "1", mainActivity)
            }else{
                val basicBeanList: MutableList<BasicBean> =
                        OneYardUtils().getHomePageList(ACacheUtil().getLoginInfo().BasicCode)
                basicList!!.removeAll(basicList!!)
                basicList!!.addAll(basicBeanList)
                homePageAdapter?.notifyDataSetChanged()
            }
        } else {
            val intent = Intent(mainActivity, LoginActivity::class.java)
            mainActivity.startActivity(intent)
        }
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            0 -> {
                val httpResponse: HttpResponse<LoginInfoBean> = data as HttpResponse<LoginInfoBean>
                if (httpResponse.State == "1") {
                    loginInfo = httpResponse.ResultData
                    ACacheUtil().setLoginInfo(loginInfo!!)
                    ACacheUtil().setUsername(username!!)
                    ACacheUtil().setPassword(password!!)

                    if (httpResponse.ResultData.BasicCode.size > 0) {
                        val basicBeanList: MutableList<BasicBean> =
                                OneYardUtils().getHomePageList(httpResponse.ResultData.BasicCode)
                        basicList!!.removeAll(basicList!!)
                        basicList!!.addAll(basicBeanList)
                        homePageAdapter?.notifyDataSetChanged()

                        initView()
                    } else {
                        ToastUtil.toast(mainActivity, "您没有使用权限，请联系管理员获取权限")
                    }

                } else {
                    ToastUtil.toast(mainActivity, httpResponse.Message)
                    val intent = Intent(mainActivity, LoginActivity::class.java)
                    mainActivity.startActivity(intent)
                }
            }
        }
    }


    fun initRecycler() {
        recyclerViewUtilKt = RecyclerViewUtilKt(mainActivity!!, homeBinding!!.homeRecycler)
        recyclerViewUtilKt?.initTableRecyclerView(3, 2)
    }

    fun initAdapter() {
        basicList = mutableListOf()
        homePageAdapter = HomePageAdapter(R.layout.item_fragment_home_page, basicList)
        recyclerViewUtilKt!!.setAdapter(homePageAdapter!!)
        homePageAdapter?.setOnItemClickListener { adapter, view, position ->
            val title = basicList!![position].BasicName
            when (title) {
                "产品信息" -> {
                    val intent = Intent(mainActivity, ProductListActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
                "批次创建" -> {
                    val intent = Intent(mainActivity, BatchListActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
                "溯源录入" -> {
                    val intent = Intent(mainActivity, BatchListActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
                "入库记录" -> {
                    val intent = Intent(mainActivity, PutStorageRecordListActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
                "出库记录" -> {
                    val intent = Intent(mainActivity, OutTreasuryRecordActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
                "库存信息" -> {
                    val intent = Intent(mainActivity, StockProductListActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
                "经销商信息" -> {
                    val intent = Intent(mainActivity, DistributorListActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
                "流通码查询" -> {
                    val intent = Intent(mainActivity, TracingCodeQueryActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
                "追溯码查询" -> {
                    val intent = Intent(mainActivity, TracingCodeQueryActivity::class.java)
                    intent.putExtra("title", title)
                    mainActivity.startActivity(intent)
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    fun initView() {
        homeBinding.username.text = "用户名：" + ACacheUtil().getLoginInfo().RoleName
        when (ACacheUtil().getLoginInfo().RoleType) {
            "EA" -> homeBinding.userRole.text = "用户角色：企业用户"
            "SA" -> homeBinding.userRole.text = "用户角色：管理员用户"
            "CU" -> homeBinding.userRole.text = "用户角色：经销商"
            "CA" -> homeBinding.userRole.text = "用户角色：集团"
        }
        homeBinding.homeMonthDeuteronomyTV.text = ACacheUtil().getLoginInfo().TraceCodetMonthCount
        homeBinding.homeAccumulativeDeuteronomyTV.text = ACacheUtil().getLoginInfo().TraceCountCount
        homeBinding.homeMonthScavengingTV.text = ACacheUtil().getLoginInfo().ScavebTraCodeMonthCount
        homeBinding.homeAccumulativeScavengingTV.text = ACacheUtil().getLoginInfo().ScavebTraCodeCount
        homeBinding.homeMonthEvaluateTV.text = ACacheUtil().getLoginInfo().MessageMonthCount
        homeBinding.homeAccumulativeEvaluateTV.text = ACacheUtil().getLoginInfo().MessageCount
    }
}